-- lost_light
-- create by zhuangsl
-- 失明不能守护格子

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 如果免疫了该技能
        if SkillM.immunitySkill(source, target, skillId) then
            SkillM.getSequence(target):immunity(source, target, skillId);
            return {};
        end

        local effectRound = -1;
        local floor = DungeonM.currentLayer();
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[2] > 0 then
            -- 不包括本回合
            effectRound = round + para[2] + 1;
        end

        -- 如果是boss
        if target:isBoss() then
            effectRound = round + 1 + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        -- 1. 威力
        local force = 1000;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        local force = math.modf(force * (addon_rate + 1000)/1000);

        -- 2. 魔力
        local magic = source:getMagic();
        -- 被对方百分比削弱
        prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 千分比
        local rate = math.modf((1000 * magic + (magic + 15) * (force - 1000)) / (magic + 15));
        rate = math.modf(para[1] * rate / force);

        -- 如果是boss
        if target:isBoss() then
            rate = rate / 2;
        end

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        local condition = {
            ["value"] = rate,
            ["end_round"] = effectRound,
        };

        -- 附加状态
        CombatStatusM.applyStatus(target, "lost_light", condition);

        EventMgr.fire(event.GRID_LOCK_STATE, {["pos"] = target:getPos(), });

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2] or -1) };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local floor = DungeonM.currentLayer();
        local max = 100;
        -- 1. 威力
        local base = 1000;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 魔力
        local magic = source:getMagic();

        -- 百分比
        local rateBase = math.modf(para[1] / 10 * math.modf((1000 * magic + (magic + 15) * (base - 1000)) / (magic + 15)) / base);
        local rateForce = math.modf(para[1] / 10 * math.modf((1000 * magic + (magic + 15) * (force - 1000)) / (magic + 15))/ force);
        --rateBase = math.min(max, rateBase);
        rateForce = math.min(max, rateForce);
        --local addon = rateForce - rateBase;
        desc = string.gsub(desc, "{reduce_accuracy}", rateForce);

        -- 回合数的显示
        local baseRound = para[2];
        local round = para[2];
        -- 增加持续时间的效果
        local prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        return desc;
    end,
};
